﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    [Serializable]
    public class SaveItemCls
    {
        public string Title;
        public string AdvImageId;
        public string IndexOrder;
        public bool  Checked=false;
    }

    public class AddAlbumItem : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AddAlbumItem";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Thêm mới Album");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddAlbumItem));
            string AlbumId = (string)System.Web.HttpContext.Current.Request["AlbumId"];
            AlbumCls AlbumCls = new AlbumBLL().CreateModel(Environment.Environments.ActiveLanguage, AlbumId);
            System.Data.DataTable dtAdvImage = new AdvImageBLL().Reading(AlbumCls.belongSite);
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "Album.js") +
                             "<input type=hidden id=txtMaxItems value=\""+dtAdvImage.Rows.Count+"\">"+
                             "      <table>";

            for(int iIndex=0;iIndex<dtAdvImage.Rows.Count;iIndex++)
            {
                string AdvImageId =(string)dtAdvImage.Rows[iIndex]["AdvImageId"];
                string Subject=(string)dtAdvImage.Rows[iIndex]["Subject"];
                RetHtml+=
                     "          <tr>" +
                     "              <td>" + ResourceBLL.GetText(WebPartId, "AdvImageCaption", "Thành phần") + "</td>" +
                     "              <td><input type=hidden id=txtAdvImageId_" + iIndex + " value=\"" + AdvImageId + "\"><input type=checkbox id=checkBox_" + iIndex + "><input id=txtTitle_" + iIndex + " class=cssTitleItemLevel1 value=\"" + Subject + "\"><input class=cssIndexOrderItem id=txtIndexOrder_" + iIndex + " value=\"" + (iIndex + 1).ToString() + "\"></td>" +
                     "          </tr>" ;
            }

            RetHtml+=

                     "          <tr>" +
                     "              <td></td>" +
                     "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveAddAlbumItem('" + AlbumId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "AddAlbumItemSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "AddAlbumItemCancelButton", "Thoát") + "\"></td>" +
                     "          </tr>" +
                     "      </table>";
            RetHtml = Environment.Environments.GetFormDialog(RetHtml, ResourceBLL.GetText(WebPartId,"SelectForAlbum", "Chọn vào album"));
            return RetHtml;
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideAddAlbumItems(string AlbumId,SaveItemCls[] SaveItems)
        {
            try
            {
                for (int iIndex = 0; iIndex < SaveItems.Length; iIndex++)
                {
                    if (SaveItems[iIndex].Checked)
                    {
                        new AlbumBLL().AddAlbumItem(
                            null,
                            AlbumId,
                            SaveItems[iIndex].Title,
                            SaveItems[iIndex].AdvImageId,
                            int.Parse(SaveItems[iIndex].IndexOrder));
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public SaveItemCls[] CreateSaveItemsObject(int Max)
        {
            SaveItemCls[] SaveItems = new SaveItemCls[Max];
            for (int iIndex = 0; iIndex < Max; iIndex++)
            {
                SaveItems[iIndex] = new SaveItemCls();
            }
            return SaveItems;
        }
    }
}